// ping ftp.gnu.org     可以获取该FTP服务器的IP地址
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>

#define PORT 21
#define REMOTE_IP "209.51.188.2"

int main(void)
{
    int sockfd;
    struct sockaddr_in addr; //定义IPv4套接口地址数据结构addr
    char buf[256];
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket created error!\n");
        exit(1);
    }
    else
    {
        printf("socket created successfully!\n");
        printf("socket id: %d\n", sockfd);
    }
    bzero(&addr, sizeof(struct sockaddr_in)); //清空表示地址的结构体变量
    addr.sin_family = AF_INET;                //设置addr的成员变量
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(REMOTE_IP);
    if (connect(sockfd, (struct sockaddr *)(&addr), sizeof(struct sockaddr)) < 0)
    {
        perror("connect error!\n");
        exit(1);
    }
    else
    {
        printf("connected successfully!\n");
        printf("remote ip:%s\n", REMOTE_IP);
        printf("remote port:%d\n", PORT);
    }
    recv(sockfd, buf, sizeof(buf), 0); //接收消息
    printf("%s\n", buf);               //输出接收到的信息
    return 0;
}